#ifndef __PROTO_H
#define __PROTO_H

#include "../serialize/encode.h"
#include "../utils/alg.h"

namespace conn::proto {

constexpr uint8_t HEAD = 0xE1;
constexpr uint8_t END = 0xEF;

constexpr uint8_t CMD_STATUS = 0x00;
constexpr uint8_t CMD_LOG = 0x01;
constexpr uint8_t CMD_OTA_START = 0x02;
constexpr uint8_t CMD_OTA_TRANSLATE = 0x03;
constexpr uint8_t CMD_OTA_COMPLETE = 0x04;

constexpr uint8_t ACTION_POS = 0x13;


template <typename... Ts>
inline uint8_t make(uint8_t *buf, uint8_t action, const Ts &...ts) {
    using namespace serialize;

    encode_int(buf, HEAD);
    encode_int(buf + 2, action);
    uint8_t len = encode_all(buf + 3, ts...);
    uint8_t sum = utils::alg::xor_sum(buf + 3, len);
    encode_int<uint8_t>(buf + 1, 5 + len);

    encode_int<uint8_t>(buf + 3 + len, sum);
    encode_int<uint8_t>(buf + 4 + len, END);
    return len + 5;
}

} // namespace conn::proto

#endif
